package Servlets.xzh;
/**
 * 项目名称：testServlet.java
 * 文件名称：testServlet.java
 * 类的描述：无
 * 创建时间：2025-07-23 16:17:34
 * 创建人：大帅比
 * <p>
 * 版权声明：本文件归 大帅比 所有，未经授权不得进行商业使用。
 */
 /*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            佛祖保佑       永无BUG
*/

import java.io.*;
import java.util.List;
import java.util.ArrayList;

import Bean.property.Expense;

import Util.GokJDBCUtil2;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@WebServlet(name = "property_pay", value = "/property_pay")
public class PropertyPayServlet extends HttpServlet {
    public void init() {
    }
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
        int pageSize = 7;
        int totalPage = 0;
        if("search".equals(req.getParameter("method"))){
            //获取参数
            String code = req.getParameter("code");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String remark = req.getParameter("remark");
            int currentPage =Integer.parseInt(req.getParameter("currentPage"));
            //sql查询
            StringBuilder sql = new StringBuilder("SELECT * FROM expense WHERE isdeleted=0");
            List<Object> params = new ArrayList<>();
            if (code != null && !code.trim().isEmpty()) {
                sql.append(" AND code like '%" + code + "%'");
            }
            if (startTime != null && !startTime.trim().isEmpty()) {
                sql.append(" AND paymenttime >= ?");
                params.add(startTime.trim());
            }
            if (endTime != null && !endTime.trim().isEmpty()) {
                sql.append(" AND paymenttime <= ?");
                params.add(endTime.trim());
            }
            if (remark != null && !remark.trim().isEmpty()) {
                sql.append(" AND remark LIKE ?");
                params.add("%" + remark.trim() + "%");
            }
            //获取总页数
            String sql_totalCount = sql.toString();
            List<Expense> expenses = GokJDBCUtil2.queryAll(sql_totalCount, Expense.class, params.toArray());
            totalPage =(int) Math.ceil( (double) expenses.size()/pageSize);
            double totalMoney = expenses.stream()
                    .mapToDouble(Expense::getMoney)
                    .sum();
            if(currentPage > totalPage){
                currentPage = totalPage;
            }
            // 分页参数
            sql.append(" ORDER BY paymenttime DESC LIMIT ?, ?");
            params.add((currentPage-1)*pageSize);
            params.add(pageSize);
            List<Expense> expense_select_default = GokJDBCUtil2.queryAll(sql.toString(), Expense.class, params.toArray());
            //返回参数
            req.setAttribute("totalPage", totalPage);
            req.setAttribute("code", code);
            req.setAttribute("startTime", startTime);
            req.setAttribute("endTime", endTime);
            req.setAttribute("remark", remark);
            req.setAttribute("currentPage", currentPage);
            req.setAttribute("expenses_select_default", expense_select_default);
            req.setAttribute("totalMoney", totalMoney);
            req.getRequestDispatcher("/pages/property_manage/property_manage_pay.jsp").forward(req, res);
        }
        else{
            int currentPage = req.getParameter("currentPage") == null ? 1 : Integer.parseInt(req.getParameter("currentPage"));
            String sql = "select * from expense where isdeleted=0";
            List<Expense>  expenses = GokJDBCUtil2.queryAll(sql, Expense.class);
            totalPage =(int) Math.ceil((double) expenses.size() / pageSize);
            double totalMoney = expenses.stream()
                    .mapToDouble(Expense::getMoney)
                    .sum();
            String sql_select_default = "select * from expense  where isdeleted=0 ORDER BY paymenttime limit ?,?";
            List<Expense>  expenses_select_default = GokJDBCUtil2.queryAll(sql_select_default, Expense.class,(currentPage-1)*pageSize, pageSize);
            req.setAttribute("expenses_select_default", expenses_select_default);
            req.setAttribute("currentPage", currentPage);
            req.setAttribute("totalPage", totalPage);
            req.setAttribute("totalMoney", totalMoney);
            req.getRequestDispatcher("/pages/property_manage/property_manage_pay.jsp").forward(req,res);
        }

    }
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String method = req.getMethod();
        try {
            if (method.equals("GET")) {
                this.doGet(req, res);
            }
            else{
                PrintWriter writer = res.getWriter();
                writer.println("{\"code\":\"400\",\"msg\":\"此方法不允许\"}");
                writer.close();
            }
        } catch (Exception e) {
            log.error("服务器处理请求时发生错误", e);
            PrintWriter writer = res.getWriter();
            writer.println("{\"code\":\"500\",\"msg\":\"服务器处理请求时发生错误\"}");
            writer.close();
        }
    }
    public void destroy() {
    }
}